package com.vstartek.dm.mongo.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import com.vstartek.dm.mongo.dao.VideoWebDao;
import com.vstartek.dm.mongo.model.Detail;
import com.vstartek.dm.mongo.model.VideoWeb;
import com.vstartek.dm.mongo.service.VideoWebService;
import com.vstartek.dm.util.Page;
import com.vstartek.dm.util.PageParam;
import com.vstartek.dm.util.SQLUtil;
import com.vstartek.dm.util.TimeUtil;
@Service
public class VideoWebServiceImpl implements VideoWebService{
	
	
	@Autowired
	VideoWebDao  videoWebDao;

	@Override
	public VideoWeb findById(Object id) {
		return videoWebDao.findById(id);
	}

	@Override
	public Page<VideoWeb> findByPage(PageParam pageParam, VideoWeb videoWeb) {
		
		Query query = new Query();
		query.fields().exclude("details").exclude("description");
		if(videoWeb.getTitle()!=null){
			query.addCriteria(new Criteria().orOperator(Criteria.where("actor").regex(".*?" + videoWeb.getTitle() + ".*"),Criteria.where("title").regex(".*?" + videoWeb.getTitle() + ".*")));
		}
		if(videoWeb.getVideoType()!=null&&(!videoWeb.getVideoType().trim().equals(""))){
			
			query.addCriteria(Criteria.where("videoType").is(videoWeb.getVideoType()));
		}
		SQLUtil.dealTimeWhere("updateTime", query, pageParam);
		
		Long num = videoWebDao.totalnum(query);
		List<VideoWeb> vlist = videoWebDao.findByList(query,pageParam);
		Page<VideoWeb> page = new Page<VideoWeb>(vlist,num,pageParam);
		return page;
	}

	@Override
	public Page<Detail> findDetailByPage(PageParam pageParam, VideoWeb videoWeb) {
		List<Detail> vlist =videoWebDao.fingdetailsByList(videoWeb.getId());
		Page<Detail> page = new Page<Detail>(vlist,(long) vlist.size(),pageParam);
		return page;
	}

	@Override
	public VideoWeb saveVideoWeb(VideoWeb videoWeb) {
		videoWeb.setUpdateTime(TimeUtil.getNowSTime());
		videoWeb.setLastUpdateTime(TimeUtil.getNowSTime());
		if(videoWebDao.save(videoWeb)){
			return videoWeb;
		}
		
		return null;
	}

	@Override
	public Detail saveDetail(VideoWeb videoWeb) {
		
		if(videoWebDao.update(videoWeb)){
			
			List<Detail> list  =videoWeb.getDetails();
			
			return list.get(list.size()-1);
		}
		
		return null;
	}

	@Override
	public boolean updateDetail(VideoWeb videoWeb) {
			
			return videoWebDao.update(videoWeb);
	}

	@Override
	public boolean updateVideoWeb(VideoWeb videoWeb) {
			videoWeb.setLastUpdateTime(TimeUtil.getNowSTime());
			return videoWebDao.update(videoWeb);
	}

	@Override
	public Detail findDetailById(Object id, Object xid) {
		
		
		return videoWebDao.findDetailById(id, xid);
	}

	@Override
	public Detail saveDetail(Object id, Detail detail) {
		return videoWebDao.saveDetail(id, detail);
	}

	@Override
	public boolean updateDetail(Object id, Detail detail) {
		// TODO Auto-generated method stub
		return videoWebDao.updateDetail(id, detail);
	}

	@Override
	public List<String> group() {
		return videoWebDao.group();
	}

}
